package com.whfc.ms.api.emp.controller;

import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.emp.dto.AppEmpGroupEmpDTO;
import com.whfc.emp.dto.AppHelmetDTO;
import com.whfc.entity.dto.OssPathDTO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.ms.api.emp.service.MsAppHelmetService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Date;

/**
 * @author sun_guodong
 * @description 人员安全帽接口
 * @date 2020-11-09
 */
@RequiresPermissions("emp/aqm")
@RestController
@RequestMapping("/ms/api/emp/helmet")
public class AppHelmetController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MsAppHelmetService msAppHelmetService;

    @GetMapping("/data/latest")
    public Result helmetLatest(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                               @RequestParam("pageNum") Integer pageNum,
                               @RequestParam("pageSize") Integer pageSize,
                               @RequestParam("deptId") Integer deptId,
                               @RequestParam(value = "corpId", required = false) Integer corpId,
                               @RequestParam(value = "groupId", required = false) Integer groupId,
                               @RequestParam(value = "keyword", required = false) String keyword) {
        logger.info("人员安全帽最新数据,loginer:{},pageNum:{},pageSize:{},deptId:{},corpId:{},groupId:{},keyword:{}",
                user.getUsername(), pageNum, pageSize, deptId, corpId, groupId, keyword);
        PageData<AppHelmetDTO> pageData = msAppHelmetService.helmetLatest(pageNum, pageSize, deptId, corpId, groupId, keyword);
        return ResultUtil.success(pageData);
    }

    @GetMapping("/data/log")
    public Result helmetLog(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                            @RequestParam("pageNum") Integer pageNum,
                            @RequestParam("pageSize") Integer pageSize,
                            @RequestParam("deptId") Integer deptId,
                            @RequestParam(value = "corpId", required = false) Integer corpId,
                            @RequestParam(value = "groupId", required = false) Integer groupId,
                            @RequestParam(value = "keyword", required = false) String keyword,
                            @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("date") Date date) {
        logger.info("人员安全帽历史数据,loginer:{},pageNum:{},pageSize:{},deptId:{},corpId:{},groupId:{},keyword:{},date:{}",
                user.getUsername(), pageNum, pageSize, deptId, corpId, groupId, keyword, date);
        PageData<AppHelmetDTO> pageData = msAppHelmetService.helmetLog(pageNum, pageSize, deptId, corpId, groupId, keyword, date);
        return ResultUtil.success(pageData);
    }

    /**
     * 安全帽最新数据导出
     *
     * @param user
     * @param deptId
     * @param corpId
     * @param groupId
     * @param keyword
     * @return
     */
    @PostMapping("/data/latest/export")
    public Result helmetLatestDataExport(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                         @RequestParam("deptId") Integer deptId,
                                         @RequestParam(value = "corpId", required = false) Integer corpId,
                                         @RequestParam(value = "groupId", required = false) Integer groupId,
                                         @RequestParam(value = "keyword", required = false) String keyword) throws IOException {
        logger.info("安全帽最新数据导出，loginer:{},deptId:{},corpId:{},groupId:{},keyword:{}",
                user.getUsername(), deptId, corpId, groupId, keyword);
        OssPathDTO ossPathDTO = msAppHelmetService.helmetLatestDataExport(deptId, corpId, groupId, keyword);
        return ResultUtil.success(ossPathDTO);
    }

    /**
     * 安全帽历史数据导出
     *
     * @param user
     * @param deptId
     * @param corpId
     * @param groupId
     * @param keyword
     * @param date
     * @return
     * @throws IOException
     */
    @PostMapping("/data/log/export")
    public Result helmetLatestDataExport(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                         @RequestParam("deptId") Integer deptId,
                                         @RequestParam(value = "corpId", required = false) Integer corpId,
                                         @RequestParam(value = "groupId", required = false) Integer groupId,
                                         @RequestParam(value = "keyword", required = false) String keyword,
                                         @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("date") Date date) throws IOException {
        logger.info("安全帽历史数据导出，loginer:{},deptId:{},corpId:{},groupId:{},keyword:{},date:{}",
                user.getUsername(), deptId, corpId, groupId, keyword, date);
        OssPathDTO ossPathDTO = msAppHelmetService.helmetLogDataExport(deptId, corpId, groupId, keyword, date);
        return ResultUtil.success(ossPathDTO);
    }

    /**
     * 查询人员轨迹列表
     *
     * @param user
     * @param deptId
     * @return
     */
    @GetMapping("/empList")
    public Result helmetEmpList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                @RequestParam("deptId") Integer deptId,
                                @RequestParam(value = "keyword", required = false) String keyword) throws IOException {
        logger.info("查询人员轨迹列表，loginer:{},deptId:{},keyword：{}", user.getUsername(), deptId, keyword);
        ListData<AppEmpGroupEmpDTO> list = msAppHelmetService.helmetEmpList(deptId, keyword);
        return ResultUtil.success(list);
    }
}
